package com.yatuhashi.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class SplashActivity extends Activity {
    private final int FP = ViewGroup.LayoutParams.FILL_PARENT; // FillParent
    private LinearLayout splashLayout; //
    private ImageView splashImage;
    private AlphaAnimation fadeIn;
    private AlphaAnimation fadeOut;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        initSplashAnimation();
        initSplashImage();
        initSplashLayout();
        
        GoToTop();
    }
    
    /**
     * 
     */
    private void initSplashAnimation() {
        fadeIn = new AlphaAnimation(0.0f, 1.0f);
        fadeIn.setDuration(1000);
        fadeOut = new AlphaAnimation(1.0f, 0.0f);
        fadeOut.setDuration(2000);
    }
    
    /**
     * 
     */
    private void initSplashLayout() {
        splashLayout = new LinearLayout(getApplicationContext());
        splashLayout.setGravity(Gravity.CENTER);
        setContentView(splashLayout);
        
        splashLayout.addView(splashImage);
        splashLayout.setBackgroundResource(R.color.white);
    }
    
    /**
     * 
     */
    private void initSplashImage() {
        splashImage = new ImageView(getApplicationContext());
        splashImage.setBackgroundResource(R.drawable.icon);
        splashImage.setLayoutParams(new LayoutParams(FP, FP));
        splashImage.setAnimation(fadeIn);
    }
    
    /**
     * 
     */
    private void GoToTop() {
        splashLayout.removeView(splashImage);
        splashLayout.addView(splashImage);
        splashImage.setAnimation(fadeOut);
        splashImage.getAnimation().setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
            
            @Override
            public void onAnimationEnd(Animation animation) {
                splashLayout.removeView(splashImage);
                splashImage.setBackgroundColor(R.color.white);
                splashLayout.addView(splashImage);
                
                Intent intent = new Intent(getApplicationContext(), GlobalActivityManager.class);
                startActivity(intent);
                finish();
            }
        });
    }
    
    /**
     * 
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }
}